package ywpt.web.action.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.util.DateTools;
import ywpt.core.util.StringTools;
import ywpt.core.web.BaseAction;
import ywpt.model.CallRecord;
import ywpt.model.JobInfo;
import ywpt.model.PeopleInfo;
import ywpt.model.SysMsgInfo;
import ywpt.service.people.PeopleService;
import ywpt.service.service.CallRecordService;
import ywpt.service.service.JobInfoService;
import ywpt.service.work.SysMsgInfoService;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class HardWareJobAction extends BaseAction implements ModelDriven<JobInfo>, Preparable {

	/**
	 * 技术部门录单Action
	 */
	private static final long serialVersionUID = 8018467431775730861L;
	private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	private static final int DEFAULT_PAGE_SIZE = 100;	
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];	
	private boolean[] cientVisit=new boolean[Constants.POPEDOM_LEN];
	private Long id;
	private JobInfo jobInfo;
	private List<JobInfo> jobInfoList = new ArrayList<JobInfo>();
	private JobInfoService jobInfoService;
	
	private int version;
	
	private Long callId;
	private String callName;
	private Long departId;
	private Long typeId;
	private Long clientId;
	private String clientName;
	private Long keshiId;
	private String keshiName;
	private Long acceptManId;
	private String acceptManName;
	private String[] assist;
	private Long sendBillId;
	private String sendBillName;
	private Long sendDepartId;
	private Long proTypeId;
	private Long stateId;
	private Long imporDegId;
	
	private PeopleService peopleService;
	
	private CallRecord callRecord;
	private CallRecordService  callRecordService;
	private List<SysMsgInfo> sysMsgInfoList = new ArrayList<SysMsgInfo>();
	private SysMsgInfoService sysMsgInfoService;
	private String msg;
	
	private String ids;
	
	//查询相关
	private String qCode;
	private String qClientName;
	private Long qTypeId;
	private Long qDepartId;
	private String qDateStart;
	private String qDateEnd;
	private int qCheckFlag;
	private String qPname;
	private String qAcceptName;
	private String qSendName;
	private int qSubmit;
	private Long qProTypeId;
	
	public String getQAcceptName() {
		return qAcceptName;
	}

	public void setQAcceptName(String acceptName) {
		qAcceptName = acceptName;
	}

	public String getQSendName() {
		return qSendName;
	}

	public void setQSendName(String sendName) {
		qSendName = sendName;
	}

	public int getQSubmit() {
		return qSubmit;
	}

	public void setQSubmit(int submit) {
		qSubmit = submit;
	}

	public Long getQProTypeId() {
		return qProTypeId;
	}

	public void setQProTypeId(Long proTypeId) {
		qProTypeId = proTypeId;
	}

	public String getQPname() {
		return qPname;
	}

	public void setQPname(String pname) {
		qPname = pname;
	}

	public String getQCode() {
		return qCode;
	}

	public void setQCode(String code) {
		qCode = code;
	}

	public String getQClientName() {
		return qClientName;
	}

	public void setQClientName(String clientName) {
		qClientName = clientName;
	}

	public Long getQTypeId() {
		return qTypeId;
	}

	public void setQTypeId(Long typeId) {
		qTypeId = typeId;
	}

	public Long getQDepartId() {
		return qDepartId;
	}

	public void setQDepartId(Long departId) {
		qDepartId = departId;
	}

	public String getQDateStart() {
		return qDateStart;
	}

	public void setQDateStart(String dateStart) {
		qDateStart = dateStart;
	}

	public String getQDateEnd() {
		return qDateEnd;
	}

	public void setQDateEnd(String dateEnd) {
		qDateEnd = dateEnd;
	}

	public int getQCheckFlag() {
		return qCheckFlag;
	}

	public void setQCheckFlag(int checkFlag) {
		qCheckFlag = checkFlag;
	}

	public CallRecord getCallRecord() {
		return callRecord;
	}

	public void setCallRecord(CallRecord callRecord) {
		this.callRecord = callRecord;
	}

	public CallRecordService getCallRecordService() {
		return callRecordService;
	}

	public void setCallRecordService(CallRecordService callRecordService) {
		this.callRecordService = callRecordService;
	}

	public PeopleService getPeopleService() {
		return peopleService;
	}

	public void setPeopleService(PeopleService peopleService) {
		this.peopleService = peopleService;
	}

	public Long getCallId() {
		return callId;
	}

	public void setCallId(Long callId) {
		this.callId = callId;
	}
	public Long getDepartId() {
		return departId;
	}

	public void setDepartId(Long departId) {
		this.departId = departId;
	}

	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public Long getClientId() {
		return clientId;
	}

	public void setClientId(Long clientId) {
		this.clientId = clientId;
	}

	public String getClientName() {
		return clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}

	public Long getKeshiId() {
		return keshiId;
	}

	public void setKeshiId(Long keshiId) {
		this.keshiId = keshiId;
	}

	public Long getAcceptManId() {
		return acceptManId;
	}

	public void setAcceptManId(Long acceptManId) {
		this.acceptManId = acceptManId;
	}

	public String getAcceptManName() {
		return acceptManName;
	}

	public void setAcceptManName(String acceptManName) {
		this.acceptManName = acceptManName;
	}

	public String[] getAssist() {
		return assist;
	}

	public void setAssist(String[] assist) {
		this.assist = assist;
	}

	public Long getSendBillId() {
		return sendBillId;
	}

	public void setSendBillId(Long sendBillId) {
		this.sendBillId = sendBillId;
	}

	public String getSendBillName() {
		return sendBillName;
	}

	public void setSendBillName(String sendBillName) {
		this.sendBillName = sendBillName;
	}

	public Long getSendDepartId() {
		return sendDepartId;
	}

	public void setSendDepartId(Long sendDepartId) {
		this.sendDepartId = sendDepartId;
	}

	public Long getProTypeId() {
		return proTypeId;
	}

	public void setProTypeId(Long proTypeId) {
		this.proTypeId = proTypeId;
	}

	public Long getStateId() {
		return stateId;
	}

	public void setStateId(Long stateId) {
		this.stateId = stateId;
	}

	public Long getImporDegId() {
		return imporDegId;
	}

	public void setImporDegId(Long imporDegId) {
		this.imporDegId = imporDegId;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public boolean[] getPopedom() {
		return popedom;
	}

	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public JobInfo getJobInfo() {
		return jobInfo;
	}

	public void setJobInfo(JobInfo jobInfo) {
		this.jobInfo = jobInfo;
	}

	public List<JobInfo> getJobInfoList() {
		return jobInfoList;
	}

	public void setJobInfoList(List<JobInfo> jobInfoList) {
		this.jobInfoList = jobInfoList;
	}

	public JobInfoService getJobInfoService() {
		return jobInfoService;
	}

	public void setJobInfoService(JobInfoService jobInfoService) {
		this.jobInfoService = jobInfoService;
	}
	
	public String getKeshiName() {
		return keshiName;
	}

	public void setKeshiName(String keshiName) {
		this.keshiName = keshiName;
	}

	public SysMsgInfoService getSysMsgInfoService() {
		return sysMsgInfoService;
	}

	public void setSysMsgInfoService(SysMsgInfoService sysMsgInfoService) {
		this.sysMsgInfoService = sysMsgInfoService;
	}
	public List<SysMsgInfo> getSysMsgInfoList() {
		return sysMsgInfoList;
	}

	public void setSysMsgInfoList(List<SysMsgInfo> sysMsgInfoList) {
		this.sysMsgInfoList = sysMsgInfoList;
	}
	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getCallName() {
		return callName;
	}

	public void setCallName(String callName) {
		this.callName = callName;
	}

	public boolean[] getCientVisit() {
		return cientVisit;
	}

	public void setCientVisit(boolean[] cientVisit) {
		this.cientVisit = cientVisit;
	}
	

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
	/*---------------------------------------------------------------------------*/
	@Override
	public JobInfo getModel() {
		return this.jobInfo;
	}

	@Override
	public void prepare() throws Exception {
		if(id!=null){
			this.jobInfo = this.jobInfoService.getJobInfoById(id);
		}else{
			this.jobInfo = new JobInfo();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(oper, popedom, OperationConstants.SERVICE_HARDWARE);
		
		HashMap<String,Operation> visit=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(visit, cientVisit, OperationConstants.SERVICE_CLIENT_VISIT);
	}
	
	
	protected void getDateList(DetachedCriteria query) {
//		if(popedom[3]){//有审核权 能看到本部门的数据
//			query.createAlias("depart", "depart");
//			query.add(Restrictions.eq("depart.id", this.getCurrentUser().getPeople().getDepart().getId()));
//		}else 
			if(!popedom[3]){//没有审核权只能看到自己新增的数据和自己受理的单子
			query.createAlias("createUser", "createUser");
			query.createAlias("acceptMan", "acceptMan");
			query.add(Restrictions.or(Restrictions.eq("createUser.id", this.getCurrentUser().getId()), 
					  Restrictions.eq("acceptMan.id", this.getCurrentUser().getPeople().getId())));
		} 
		query.add(Restrictions.eq("deleteFlag", false));
		query.add(Restrictions.eq("mark", 2));
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = jobInfoService.getJobInfoCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.desc("id"));
		query.setProjection(null);
		jobInfoList.addAll(jobInfoService.getJobInfos(query, pageSize, rowStartEnd[0]));
	}
 
 

	protected void showInfo() {
		if(this.jobInfo.getCallRecord()!=null){
			this.setCallId(this.jobInfo.getCallRecord().getId());
			this.setCallName(this.jobInfo.getCallRecord().getRecordCode());
		}
		if(this.jobInfo.getDepart()!=null){
			this.setDepartId(this.jobInfo.getDepart().getId());
		}
		if(this.jobInfo.getType()!=null){
			this.setTypeId(this.jobInfo.getType().getId());
		}
		if(this.jobInfo.getClient()!=null){
			this.setClientId(this.jobInfo.getClient().getId());
			this.setClientName(this.jobInfo.getClient().getClientName());
		}
		if(this.jobInfo.getKeshi()!=null){
			this.setKeshiId(this.jobInfo.getKeshi().getId());
			this.setKeshiName(this.jobInfo.getKeshi().getName());
		}
		if(this.jobInfo.getAcceptMan()!=null){
			this.setAcceptManId(this.jobInfo.getAcceptMan().getId());
			this.setAcceptManName(this.jobInfo.getAcceptMan().getPeopleName());
		}
		if(this.jobInfo.getSendBill()!=null){
			this.setSendBillId(this.jobInfo.getSendBill().getId());
			this.setSendBillName(this.jobInfo.getSendBill().getPeopleName());
		}
		if(this.jobInfo.getSendDepart()!=null){
			this.setSendDepartId(this.jobInfo.getSendDepart().getId());
		}
		if(this.jobInfo.getProType()!=null){
			this.setProTypeId(this.jobInfo.getProType().getId());
		}
		if(this.jobInfo.getState()!=null){
			this.setStateId(this.jobInfo.getState().getId());
		}
		if (this.jobInfo.getImporDeg()!=null) {
			this.setImporDegId(this.jobInfo.getImporDeg().getId());
		}
		this.sysMsgInfoList.addAll(this.sysMsgInfoService.getMsgs(10, this.jobInfo.getId()));
		getLimit();
	}
 
	protected void getLimit(){
		@SuppressWarnings("unused")		
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", this.jobInfo.getJobProcInfoSet().size(), DEFAULT_PAGE_SIZE);
	}

/*==================================================================================*/
	
	public String list() {
		DetachedCriteria query = DetachedCriteria.forClass(JobInfo.class);
			this.getDateList(query);
		return SUCCESS;
	}
	
	public String input() {
		PeopleInfo peopleInfo = this.getCurrentUser().getPeople();
		if(peopleInfo!=null){
			this.setAcceptManId(peopleInfo.getId());
			this.setAcceptManName(peopleInfo.getPeopleName());
			this.setDepartId(peopleInfo.getDepart().getId());
			this.setSendBillId(peopleInfo.getId());
			this.setSendBillName(peopleInfo.getPeopleName());
			this.setSendDepartId(peopleInfo.getDepart().getId());
		}
		if (callId!=null) {
			this.callRecord = this.callRecordService.getCallRecordById(callId);
			this.setCallName(callRecord.getRecordCode());
			if (callRecord.getClient()!=null) {
				this.clientId = callRecord.getClient().getId();
				this.clientName = callRecord.getClient().getClientName();				
			}
			if (callRecord.getKeshi()!=null) {
				this.keshiId = callRecord.getKeshi().getId();
				this.keshiName = callRecord.getKeshi().getName();				
			}
			this.jobInfo.setPhone(callRecord.getManPhone());
			this.jobInfo.setRequDesc(callRecord.getQuestions());
		}
		this.jobInfo.setStartDate(new Date());
		this.jobInfo.setEndDate(new Date());
		this.jobInfo.setBillDate(new Date());
		this.setStateId((long)11);
		this.setImporDegId((long)50);
		
		return INPUT;
	}
	public String edit() {
		this.jobInfo = this.jobInfoService.getJobInfoById(id);
		showInfo();
		return INPUT;
	}

	public String save() throws Exception{
		try 
		{
			if (id == null) 
			{
				if(jobInfoService.checkJobCode(jobInfo.getJobCode(),2)){
					jobInfo.setSysCode(this.jobInfoService.createSysCode(2));
					jobInfo.setMark(2);
					jobInfo.setCreateUser(this.getCurrentUser());
					jobInfo.setCreateDate(new Date());
					jobInfoService.saveJobInfo(jobInfo,callId,departId,typeId,clientId,keshiId,acceptManId,assist,sendDepartId,sendBillId,proTypeId,stateId,imporDegId);
					if (callId!=null) {
						finishCall(callId);
					}
					this.addActionMessage(SystemMessage.SAVE_SUCCESS);					
				}else {
					this.addActionMessage("单号已存在，请重新输入！");
				}

			} 
			else 
			{   
				if(this.getVersion()==jobInfo.getVersion())
				{
					if (!jobInfo.getSubmitFlag()) {
						if (!jobInfo.getCheckFlag()) {
							jobInfo.setModifyUser(this.getCurrentUser());
							jobInfo.setModifyDate(new Date());
							jobInfoService.updateJobInfo(jobInfo,callId,departId,typeId,clientId,keshiId,acceptManId,assist,sendDepartId,sendBillId,proTypeId,stateId,imporDegId);
							if (callId!=null) {
								callRecord=callRecordService.getCallRecordById(callId);
								callRecord.setState(2);
								callRecordService.updateCallRecord(callRecord, null, null, null,null,null,null);
							}
							this.addActionMessage(SystemMessage.UPDATE_SUCCESS);						
						} else {
							this.addActionMessage(SystemMessage.AUDITING_SAVE);
						}
					} else {
						this.addActionMessage(SystemMessage.SUBMIT_SAVE);
					}
				}
				else
					this.addActionMessage(SystemMessage.UPDATE_FAIL);
			}
			showInfo();
			return SUCCESS;
		} 
		catch (UniqueConstraintException e) 
		{
				this.addActionError(e.getMessage());
			return INPUT;
		}
	}
	
	public String delete() {
		if(id!=null){
			jobInfo = jobInfoService.getJobInfoById(id);
				if (!jobInfo.getCheckFlag()) {
					if (!jobInfo.getSubmitFlag()) {
						if (jobInfo.getCallRecord()!=null) {
							callRecord = jobInfo.getCallRecord();
							callRecord.setState(1);
							callRecordService.updateCallRecord(callRecord, null, null, null,null,null,null);
						}
						this.jobInfoService.removeJobInfo(id);	
					} else {
						this.addActionMessage(SystemMessage.SUBMIT_SAVE);
					}	
				} else {
					this.addActionMessage(SystemMessage.DELETE_AUDITING_FAIL);
				}
		}else{
			this.addActionMessage(SystemMessage.NO_OBJECT);
		}showInfo();
		return SUCCESS;
	}

	//分派 assign
	public String assign() {
		if (id==null) {
			this.addActionMessage(SystemMessage.NO_OBJECT);
		} else {
			this.jobInfo = this.jobInfoService.getJobInfoById(id);
			if (!jobInfo.getCheckFlag()) {
				if (!jobInfo.getSubmitFlag()) {
					if (sendBillId!=null) {
						jobInfoService.updateJobInfo(jobInfo, null, null, null, null, null, null, null,sendDepartId, sendBillId, null, null, null);
						sysMsgInfoService.invalidMsg(10, id);
						addSysMsg(jobInfo,sendBillId);
						this.addActionMessage("派单成功！");
					}else 
						this.addActionMessage("请选择派单人！");
				} else 
					this.addActionMessage(SystemMessage.SUBMIT_SAVE);
			} else 
				this.addActionMessage(SystemMessage.AUDITING_SAVE);
		}
		this.showInfo();
		return SUCCESS;
	}
	
	//提交submit
	public String submit() throws ServletException{
		try {
			if (id==null) {
				this.addActionError(SystemMessage.NO_OBJECT);
			} else {
				this.jobInfo = this.jobInfoService.getJobInfoById(id);
				if (!jobInfo.getCheckFlag()) {
					if (!jobInfo.getSubmitFlag()) {
						jobInfo.setSubmitFlag(true);
						jobInfoService.updateJobInfo(jobInfo, null, null, null, null, null, null, null,null, null, null, null, null);
						sysMsgInfoService.invalidMsg(10, id);
						addSubmitSysMsg(jobInfo);
						this.addActionMessage(SystemMessage.SUMBIT_SUCCESS);
					} else 
						this.addActionMessage(SystemMessage.SUBMIT_SAVE);
				} else 
					this.addActionMessage(SystemMessage.AUDITING_SAVE);
			}
			this.showInfo();
			return SUCCESS;
		} catch (Exception e) {
			this.addActionMessage(e.getMessage());
			return SUCCESS;
		}
	}
	
	
	//退回disSubmit
	public String disSubmit(){
		if (id==null) {
			this.addActionError(SystemMessage.NO_OBJECT);
		} else {
			this.jobInfo = this.jobInfoService.getJobInfoById(id);
			if (!jobInfo.getCheckFlag()) {
				if (jobInfo.getSubmitFlag()) {
					jobInfo.setSubmitFlag(false);
					this.jobInfoService.updateJobInfo(jobInfo, null, null, null, null, null,null, null, null, null, null, null, null);
					sysMsgInfoService.invalidMsg(10, id);
					addDisSubmitSysMsg(jobInfo,msg);
					this.addActionMessage(SystemMessage.DISSUMBIT_SUCCESS);
				} else 
					this.addActionMessage(SystemMessage.DISSUMBIT__REPEAT);
			} else 
				this.addActionMessage(SystemMessage.AUDITING_SAVE);
		}
		
		this.showInfo();
		return SUCCESS;
	}
	
	public String check() {
		if (id==null) {
			this.addActionMessage(SystemMessage.NO_OBJECT);
		} else {
			this.jobInfo=this.jobInfoService.getJobInfoById(id);
			if (!jobInfo.getCheckFlag()) {
				jobInfo.setCheckFlag(true);
				jobInfoService.updateJobInfo(jobInfo, null, null, null, null, null, null,null, null, null, null, null, null);
				sysMsgInfoService.invalidMsg(10, id);
				this.addActionMessage(SystemMessage.AUDITING_SUCCESS);
			} else {
				this.addActionMessage(SystemMessage.AUDITING_REPEAT);
			}
		}
		showInfo();
		return SUCCESS;
	}

	public String disCheck() {
		if (id==null) {
			this.addActionMessage(SystemMessage.NO_OBJECT);
		} else {
			this.jobInfo=this.jobInfoService.getJobInfoById(id);
			if (jobInfo.getCheckFlag()) {
				jobInfo.setCheckFlag(false);
				jobInfoService.updateJobInfo(jobInfo, null, null, null, null, null, null,null, null, null, null, null, null);
				this.addActionMessage(SystemMessage.DISAUDITING_SUCCESS);
			} else {
				this.addActionMessage(SystemMessage.DISAUDITING_REPEAT);
			}
		}		
		showInfo();
		return SUCCESS;
	}

	protected void addSysMsg(JobInfo jobInfo , Long sendBillId) {
		SysMsgInfo sysMsgInfo = new SysMsgInfo();
		sysMsgInfo.setMsgTitle("硬件服务单");
		sysMsgInfo.setMsgContent(peopleService.getPeopleInfoById(sendBillId).getPeopleName()+"向你分派了一张服务单！");
		sysMsgInfo.setReceiveUser(jobInfo.getAcceptMan().getId() +",");
		sysMsgInfo.setBusinessType(10);
		sysMsgInfo.setBusinessId(jobInfo.getId());
		sysMsgInfo.setUrl("/service/hardWare/edit-"+jobInfo.getId()+".action");
		sysMsgInfo.setCreateDate(new Date());
		sysMsgInfo.setCreater(this.getCurrentUser());
		sysMsgInfoService.saveSysMsgInfo(sysMsgInfo);
	}
	
	protected void addSubmitSysMsg(JobInfo jobInfo){
		SysMsgInfo sysMsgInfo = new SysMsgInfo();
		sysMsgInfo.setMsgTitle("硬件服务单");
		sysMsgInfo.setMsgContent(jobInfo.getAcceptMan().getPeopleName()+"向你提交了一张服务单，等待审核中！");
		sysMsgInfo.setReceiveUser(jobInfo.getSendBill().getId()+",");
		sysMsgInfo.setBusinessType(10);
		sysMsgInfo.setBusinessId(jobInfo.getId());
		sysMsgInfo.setUrl("/service/hardWare/edit-"+jobInfo.getId()+".action");
		sysMsgInfo.setCreateDate(new Date());
		sysMsgInfo.setCreater(this.getCurrentUser());
		sysMsgInfoService.saveSysMsgInfo(sysMsgInfo);
	}
	
	protected void addDisSubmitSysMsg(JobInfo jobInfo,String msg){
		SysMsgInfo sysMsgInfo = new SysMsgInfo();
		sysMsgInfo.setMsgTitle("硬件服务单");
		if (msg!=null) {
			sysMsgInfo.setMsgContent(this.getCurrentUser().getPeople().getPeopleName()+"给你退回了一张服务单！原因："+msg);
		} else {
			sysMsgInfo.setMsgContent(this.getCurrentUser().getPeople().getPeopleName()+"给你退回了一张服务单！");
		}
		sysMsgInfo.setReceiveUser(jobInfo.getAcceptMan().getId()+",");
		sysMsgInfo.setBusinessType(10);
		sysMsgInfo.setBusinessId(jobInfo.getId());
		sysMsgInfo.setUrl("/service/hardWare/edit-"+jobInfo.getId()+".action");
		sysMsgInfo.setCreateDate(new Date());
		sysMsgInfo.setCreater(this.getCurrentUser());
		sysMsgInfoService.saveSysMsgInfo(sysMsgInfo);
	}
	
	public String search() throws Exception {
		DetachedCriteria query = DetachedCriteria.forClass(JobInfo.class);
			if (!StringTools.isNullOrSpace(this.qCode)) {
				query.add(Restrictions.like("jobCode", "%"+this.qCode+"%"));
			}
			if (!StringTools.isNullOrSpace(this.qClientName)) {
				query.createAlias("client", "client").add(Restrictions.like("client.clientName", "%"+this.qClientName+"%"));
			}
			if (null!=this.qTypeId) {
				query.createAlias("type", "type").add(Restrictions.eq("type.id", this.qTypeId));
			}
			if (null!=this.qDepartId) {
				query.createAlias("depart", "depart").add(Restrictions.eq("depart.id", this.qDepartId));
			}
			if(!StringTools.isNullOrSpace(this.qDateStart)&& !StringTools.isNullOrSpace(this.qDateEnd))
			{				
				if(FORMAT.parse(this.qDateStart).before(FORMAT.parse(this.qDateEnd)))
					query.add(Restrictions.between("billDate",FORMAT.parse(this.qDateStart),DateTools.addDay(FORMAT.parse(this.qDateEnd), 0)));
				else  
					query.add(Restrictions.between("billDate",FORMAT.parse(this.qDateEnd),DateTools.addDay(FORMAT.parse(this.qDateStart),0)));
			}
			if (this.qCheckFlag==1) {//审核
				query.add(Restrictions.eq("checkFlag", true));
			} else if(this.qCheckFlag==2){//未审
				query.add(Restrictions.eq("checkFlag", false));
			}
			if (!StringTools.isNullOrSpace(this.qPname)) {
				query.createAlias("createUser", "createUser").add(Restrictions.like("createUser.fullName", "%"+this.qPname+"%"));
			}
			if (!StringTools.isNullOrSpace(this.qAcceptName)) {
				query.createAlias("acceptMan", "acceptMan").add(Restrictions.like("acceptMan.peopleName", "%"+this.qAcceptName+"%"));
			}
			if (!StringTools.isNullOrSpace(this.qSendName)) {
				query.createAlias("sendBill", "sendBill").add(Restrictions.like("sendBill.peopleName", "%"+this.qSendName+"%"));
			}
			if (this.qSubmit==1) {//已提交
				query.add(Restrictions.eq("submitFlag", true));
			} else if(this.qSubmit==2){//未提交
				query.add(Restrictions.eq("submitFlag", false));
			}
			if (null!=qProTypeId) {
				query.createAlias("proType", "proType").add(Restrictions.eq("proType.id", qProTypeId));
			}
			this.getDateList(query);
		return SUCCESS;
	}

	
	public String checkList() {
		if(!StringTools.isNullOrSpace(this.ids)){
			String[] idStr = this.ids.split(";"); 
			int i=0;
			for(String item: idStr){
				Long cId = Long.parseLong(item);
				jobInfo = this.jobInfoService.getJobInfoById(cId);
				if (!jobInfo.getCheckFlag()&&jobInfo.getSubmitFlag()) {
					jobInfo.setCheckFlag(true);
					this.jobInfoService.updateJobInfo(jobInfo, null, null, null, null, null, null, null, null, null, null, null, null);
					sysMsgInfoService.invalidMsg(10, cId);
				}else{
					i++;
				}
			}
			if(i>0){
				this.addActionMessage("当前选择的记录有"+i+"条已经审核或未提交！");
			}
		}
		return SUCCESS;
	}

	public String submitList() {
		if(!StringTools.isNullOrSpace(this.ids)){
			String[] idStr = this.ids.split(";"); 
			int i=0;
			for(String item: idStr){
				Long cId = Long.parseLong(item);
				jobInfo = this.jobInfoService.getJobInfoById(cId);
				if (!jobInfo.getSubmitFlag()) {
					jobInfo.setSubmitFlag(true);
					this.jobInfoService.updateJobInfo(jobInfo, null, null, null, null, null, null, null, null, null, null, null, null);
					sysMsgInfoService.invalidMsg(10, cId);
					addSubmitSysMsg(jobInfo);
				}else{
					i++;
				}
			}
			if(i>0){
				this.addActionMessage("当前选择的记录有"+i+"条已经提交！");
			}
		}
		return SUCCESS;
	}

	protected void finishCall(Long callId) {
		//改变电话记录的状态为完成
		CallRecord callRecord = callRecordService.getCallRecordById(callId);
		callRecord.setState(2);
		this.callRecordService.updateCallRecord(callRecord, null, null, null,null,null,null);
		/**
		 * 电话提醒失效
		 */
		sysMsgInfoService.invalidMsg(11, callId);
	}
}
